package com.zhaotianhao.serviceproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaotianhao.serviceproduct.entity.SpecGroup;
import com.zhaotianhao.serviceproduct.mapper.SpecGroupMapper;
import com.zhaotianhao.serviceproduct.service.ISpecGroupService;
import com.zhaotianhao.serviceproduct.vo.SpecGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 规格参数的分组表，每个商品分类下有多个规格参数组 服务实现类
 * </p>
 *
 * @author Z-7
 * @since 2022-04-27
 */
@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements ISpecGroupService {

    /**
     * 得到所有的组内容
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> getListByCid(Long cid) {
        List<SpecGroup> list = list(new QueryWrapper<SpecGroup>().eq("cid", cid));
        return list;
    }

    /**
     * 得到所有的组内容
     *
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroupVO> getSpecGroupVoListByCid(Long cid) {
        List<SpecGroup> specGroupList = getListByCid(cid);
        List<SpecGroupVO> groups = new ArrayList<>();
        for (SpecGroup group : specGroupList) {
            SpecGroupVO specGroupVO = toSpecGroupVo(group);
            groups.add(specGroupVO);
        }
        return groups;
    }

    private SpecGroupVO toSpecGroupVo(SpecGroup group) {
        SpecGroupVO specGroupVO = new SpecGroupVO();
        BeanUtils.copyProperties(group,specGroupVO);
        return specGroupVO;
    }
}
